package cn.edu.ncut.cs.springboot.petmanagementsystem.controller;

import cn.edu.ncut.cs.springboot.petmanagementsystem.common.PageResult;
import cn.edu.ncut.cs.springboot.petmanagementsystem.common.Result;
import cn.edu.ncut.cs.springboot.petmanagementsystem.pojo.Employee;
import cn.edu.ncut.cs.springboot.petmanagementsystem.service.StaffService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/staff")
@Tag(name = "猫舍员工管理模块")
public class StaffController {

    @Autowired
    private StaffService staffService;

    @Operation(summary = "新增员工信息")
    @PostMapping("/create")
    public Result<?> createStaff(@RequestBody Employee employee) {
        try {
            boolean success = staffService.createStaff(employee);
            return success ? Result.success("新增成功") : Result.error("新增失败");
        } catch (IllegalArgumentException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            log.error("新增员工失败", e);
            return Result.error("新增失败: " + e.getMessage());
        }
    }

    @Operation(summary = "修改员工资料")
    @PutMapping("/update/{id}")
    public Result<?> updateStaff(
            @Parameter(description = "员工ID") @PathVariable Long id,
            @RequestBody Employee employee) {
        try {
            employee.setId(id);
            boolean success = staffService.updateStaff(employee);
            return success ? Result.success("更新成功") : Result.error("更新失败");
        } catch (Exception e) {
            log.error("更新员工信息失败", e);
            return Result.error("更新失败: " + e.getMessage());
        }
    }

    @Operation(summary = "查询员工信息列表")
    @GetMapping("/list")
    public PageResult<Employee> getStaffList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "员工姓名") @RequestParam(required = false) String employeeName,
            @Parameter(description = "部门") @RequestParam(required = false) String department) {
        try {
            IPage<Employee> page = staffService.getStaffList(pageNum, pageSize, employeeName, department);
            return PageResult.success(page);
        } catch (Exception e) {
            log.error("查询员工列表失败", e);
            return PageResult.pageError("查询失败: " + e.getMessage());
        }
    }

    @Operation(summary = "查看单个员工的详细资料")
    @GetMapping("/{id}")
    public Result<Employee> getStaffDetail(
            @Parameter(description = "员工ID") @PathVariable Long id) {
        try {
            Employee employee = staffService.getStaffById(id);
            if (employee != null) {
                return Result.success(employee);
            }
            return Result.error("员工不存在");
        } catch (Exception e) {
            log.error("查询员工详情失败", e);
            return Result.error("查询失败");
        }
    }

    @Operation(summary = "调整员工的工作排班")
    @PutMapping("/schedule/{id}")
    public Result<?> updateSchedule(
            @Parameter(description = "员工ID") @PathVariable Long id,
            @Parameter(description = "排班信息") @RequestParam String schedule) {
        try {
            boolean success = staffService.updateSchedule(id, schedule);
            return success ? Result.success("更新成功") : Result.error("更新失败");
        } catch (Exception e) {
            log.error("更新排班失败", e);
            return Result.error("更新失败: " + e.getMessage());
        }
    }

    @Operation(summary = "删除员工信息记录")
    @DeleteMapping("/delete/{id}")
    public Result<?> deleteStaff(
            @Parameter(description = "员工ID") @PathVariable Long id) {
        try {
            boolean success = staffService.deleteStaff(id);
            return success ? Result.success("删除成功") : Result.error("删除失败");
        } catch (Exception e) {
            log.error("删除员工失败", e);
            return Result.error("删除失败: " + e.getMessage());
        }
    }
}
